package SwingView.Announcement;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.*;

import JDBC.InstructorAnnouncementActivities;
import SwingController.ALDownloadFile;
import SwingController.CmsPresenter;
import SwingModel.CmsAnnouncement;
import SwingModel.CmsLecture;
import SwingModel.CmsUser;
import SwingView.CmsMainView;
import SwingView.CmsContentPanelModel;
import SwingView.Lecture.ALLectureDelete;
public class CmsCPInstructorAnnouncement extends CmsContentPanelModel{

	private JLabel jlTitle;
	private JPanel jpContent;
	private NewAnnouncementPanel newAnmPanel;
	
	
	public CmsCPInstructorAnnouncement(){
		
		//helper panel to center the title panel
		JPanel jp = new JPanel();
		jp.setPreferredSize(new Dimension(650,220));
		jp.setOpaque(false);
		jp.setBorder(BorderFactory.createEmptyBorder());
		GridBagLayout gbl_jp = new GridBagLayout();
		jp.setLayout(gbl_jp);
		
		jlTitle = new JLabel("Announcements");
		jlTitle.setFont(jlTitle.getFont().deriveFont(20.0f).deriveFont(Font.BOLD));
		jlTitle.setBorder(BorderFactory.createEmptyBorder(0, 8, 10, 8));
		GridBagConstraints gbc_jlTitle = new GridBagConstraints();
		gbc_jlTitle.insets = new Insets(10, 0, 0, 5);
		gbc_jlTitle.gridx = 0;
		gbc_jlTitle.gridy = 0;
		jp.add(jlTitle, gbc_jlTitle);
		
		newAnmPanel = new NewAnnouncementPanel();
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.insets = new Insets(0, 0, 15, 0);
		gbc_panel_1.gridx = 0;
		gbc_panel_1.gridy = 1;
		gbc_panel_1.weighty = 1.0;
		jp.add(newAnmPanel, gbc_panel_1);
		
		//content panel
		jpContent = new JPanel();
		jpContent.setLayout(new GridBagLayout());
		jpContent.setOpaque(false);
		
		//Set up the scroll bar
		JScrollPane scroll = new JScrollPane(jpContent);
		scroll.setBorder(BorderFactory.createEmptyBorder());
		scroll.setOpaque(true);
		scroll.setPreferredSize(new Dimension(650,450));
		scroll.setBackground(Color.WHITE);
		
		//set up student Announcement panel and add components
		setLayout(new BorderLayout());
		add(jp,BorderLayout.NORTH);//Add title People
		
		add(scroll,BorderLayout.WEST);//Add People List
	}
	
	public void displayListOfAnnouncements(List<CmsAnnouncement> listOfAnnouncements){
		jpContent.removeAll();
		jpContent.revalidate();
		jpContent.repaint();
		int i = 0;
		for(CmsAnnouncement announcement : listOfAnnouncements){
			GridBagConstraints c = new GridBagConstraints();
			c.gridy = i;
			c.insets = new Insets(0, 0, 8, 0);
			
			jpContent.add(new AnnouncementEntryRow(announcement),c);
			
			i++;
		}
		
		//empty space at bottom - used to fill the bottom of the page
		//when the list is too short
		JPanel p = new JPanel();
		p.setOpaque(false);
		p.setBackground(Color.WHITE);
		GridBagConstraints c_p = new GridBagConstraints();
		c_p.gridy = i;
		c_p.fill = GridBagConstraints.VERTICAL;
		c_p.weightx=1.0;
		c_p.weighty=1.0;
		jpContent.add(p,c_p);
		
		jpContent.revalidate();
		jpContent.repaint();
	}
	
	public CmsAnnouncement getComposedAnnouncement(){
		return newAnmPanel.getComposedAnnouncement();
	}
	
	public boolean validateAllInput(){
		return newAnmPanel.validateAllInputs();
	}
	
	public void clearAllInput(){
		newAnmPanel.clearAllInput();
	}
	
	
	private class AnnouncementEntryRow extends JPanel{
		final static private int ROW_WIDTH = 550;
		final static private int ROW_HEIGHT = 50;
		
		final static private int DATE_NAME_COLUMN_SIZE = 100;
		private JButton viewButton;
		private JButton deleteButton;
		private JPanel jpAnmContent;
		
		private CmsAnnouncement anm;
		
		public AnnouncementEntryRow(CmsAnnouncement anm){

			JLabel jlTitle;
			JLabel postedDate; //avai date
			JLabel pdTitle;
			JLabel expDate; //exp date
			JLabel edTitle;
			
			SimpleDateFormat sdf;
			
			this.anm = anm;
			
			//Set up file name label
			String anmTitle = anm.getAnnouncementTitle();
			jlTitle = new JLabel(anmTitle);
			jlTitle.setFont(jlTitle.getFont().deriveFont(15.0f).deriveFont(Font.BOLD));
			jlTitle.setBorder(BorderFactory.createEmptyBorder(5, 8, 5, 5));
			
			//Set up the "Available date" title label
			pdTitle = new JLabel("Posted Date: ");
			pdTitle.setFont(pdTitle.getFont().deriveFont(10.0f).deriveFont(Font.BOLD));
			pdTitle.setBorder(BorderFactory.createEmptyBorder(5, 8, 0, 5));
			
			//set up avai date Label
			postedDate = new JLabel(CmsPresenter.sdf.format(new Date(anm.getCreatedDTM().getTime())));
			postedDate.setFont(jlTitle.getFont().deriveFont(10.0f));
			postedDate.setForeground(new Color(40,40,40));
			postedDate.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
			
			//set up view button
			viewButton = new JButton ("View");
			viewButton.setFont(viewButton.getFont().deriveFont(12.0f));
			viewButton.setForeground(Color.BLUE);
			viewButton.setOpaque(false);
			viewButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			viewButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			
			//set up delete button
			deleteButton = new JButton ("Delete");
			deleteButton.setFont(viewButton.getFont().deriveFont(12.0f));
			deleteButton.setForeground(Color.BLUE);
			deleteButton.setOpaque(false);
			deleteButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			deleteButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			
			//Set up the panel
			setPreferredSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
			setMaximumSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
			if(anm.getIsRead()) setBackground(Color.WHITE);
			else setBackground(Color.LIGHT_GRAY);
			setBorder(BorderFactory.createLineBorder(new Color(170,170,170), 1));
			setOpaque(true);
			setLayout(new GridBagLayout());
			
			//add the Title
			GridBagConstraints cf = new GridBagConstraints();
			cf.fill = GridBagConstraints.HORIZONTAL;
			cf.anchor = GridBagConstraints.FIRST_LINE_START;
			cf.gridwidth = 4;
			add(jlTitle,cf);
			
			//add "Posted date" title:
			GridBagConstraints cp = new GridBagConstraints();
			cp.insets = new Insets(0,0,10,0);
			cp.gridx = 0;
			cp.gridy = 1;
			cp.anchor = GridBagConstraints.LAST_LINE_START;
			add(pdTitle,cp);
			
			//add the posted date
			GridBagConstraints cd = new GridBagConstraints();
			cd.insets = new Insets(0,0,10,0);
			cd.gridx = 1;
			cd.gridy = 1;
			cd.anchor = GridBagConstraints.LAST_LINE_START;
			add(postedDate,cd);
			
			
			//add the view button
			GridBagConstraints cb = new GridBagConstraints();
			cb.gridx = 2;
			cb.gridy = 1;
			cb.weightx = 1.0;
			cb.anchor = GridBagConstraints.LAST_LINE_END;
			add(viewButton,cb);
			
			//add the delete button
			GridBagConstraints cdel = new GridBagConstraints();
			cdel.gridx = 3;
			cdel.gridy = 1;
			cdel.anchor = GridBagConstraints.LAST_LINE_END;
			add(deleteButton,cdel);
			
			jpAnmContent = new JPanel(new BorderLayout());
			GridBagConstraints c = new GridBagConstraints();
			c.gridx = 0;
			c.gridy = 2;
			c.gridwidth = 4;
			c.weighty = 1.0;
			c.insets = new Insets(0,10,10,10);
			c.fill = GridBagConstraints.BOTH;
			c.anchor = GridBagConstraints.FIRST_LINE_START;
			add(jpAnmContent,c);
			
			//Add View AL:
			viewButton.addActionListener(new ViewActionListener());
			deleteButton.addActionListener(new ALAnnouncementDelete(anm));
		}
		
		public class ViewActionListener implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				if(viewButton.getText().equals("View")){
					viewButton.setText("Close");
					JTextArea jtfContent = new JTextArea(anm.getAnnouncementContent());
					jtfContent.setEditable(false);
					JScrollPane scr = new JScrollPane(jtfContent);
					scr.setPreferredSize(new Dimension(500,200));
					jpAnmContent.add(scr,BorderLayout.CENTER);
					jpAnmContent.revalidate();
					setPreferredSize(new Dimension(ROW_WIDTH, ROW_HEIGHT+150));
					setMaximumSize(new Dimension(ROW_WIDTH, ROW_HEIGHT+150));

				}
				else{
					viewButton.setText("View");
					jpAnmContent.removeAll();
					jpAnmContent.revalidate();
					setPreferredSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
					setMaximumSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
					setBackground(Color.WHITE);
				}
				
			}
			
		}
		
	}
	
	public class NewAnnouncementPanel extends JPanel {

		private static final int PANEL_WIDTH = 600;
		private static final int PANEL_HEIGHT = 190;
		
		public JTextArea jtfTitle;
		public JTextArea jtfContent;
		public JButton clearButton;
		public JButton postButton;
		
		public NewAnnouncementPanel(){
			setPreferredSize(new Dimension(PANEL_WIDTH,PANEL_HEIGHT));
			setMinimumSize(new Dimension(PANEL_WIDTH,PANEL_HEIGHT));
			setMaximumSize(new Dimension(PANEL_WIDTH,PANEL_HEIGHT));
			setOpaque(false);
			setForeground(new Color(210,210,210));
			setLayout(new GridBagLayout());
			
			
			JLabel jlbNewAnnouncement = new JLabel("<HTML><U>New Announcement:<U><HTML>");
			jlbNewAnnouncement.setFont(new Font("Lucida Grande", Font.BOLD, 17));
			GridBagConstraints gbc_jlbNewAnnouncement = new GridBagConstraints();
			gbc_jlbNewAnnouncement.anchor = GridBagConstraints.FIRST_LINE_START;
			gbc_jlbNewAnnouncement.gridwidth = 2;
			gbc_jlbNewAnnouncement.insets = new Insets(10, 10, 5, 0);
			gbc_jlbNewAnnouncement.gridx = 0;
			gbc_jlbNewAnnouncement.gridy = 0;
			add(jlbNewAnnouncement, gbc_jlbNewAnnouncement);
			
			JLabel lblTitle = new JLabel("Title: ");
			lblTitle.setFont(new Font("Lucida Grande", Font.BOLD, 13));
			GridBagConstraints gbc_lblTitle = new GridBagConstraints();
			gbc_lblTitle.insets = new Insets(0, 10, 5, 5);
			gbc_lblTitle.anchor = GridBagConstraints.LINE_START;
			gbc_lblTitle.gridx = 0;
			gbc_lblTitle.gridy = 1;
			add(lblTitle, gbc_lblTitle);
			
			jtfTitle = new JTextArea();
			jtfTitle.setLineWrap(true);
			jtfTitle.setWrapStyleWord(true);
			
			JScrollPane spTitle = new JScrollPane(jtfTitle);
			spTitle.setPreferredSize(new Dimension(400,20));
			GridBagConstraints gbc_spTitle = new GridBagConstraints();
			gbc_spTitle.insets = new Insets(0, 0, 5, 10);
			gbc_spTitle.fill = GridBagConstraints.BOTH;
			gbc_spTitle.gridx = 1;
			gbc_spTitle.gridy = 1;
			add(spTitle, gbc_spTitle);
			
			JLabel lblContent = new JLabel("Content: ");
			lblContent.setFont(new Font("Lucida Grande", Font.BOLD, 13));
			GridBagConstraints gbc_lblContent = new GridBagConstraints();
			gbc_lblContent.weightx = 0.05;
			gbc_lblContent.anchor = GridBagConstraints.FIRST_LINE_START;
			gbc_lblContent.insets = new Insets(0, 10, 5, 5);
			gbc_lblContent.gridx = 0;
			gbc_lblContent.gridy = 2;
			add(lblContent, gbc_lblContent);
			
			jtfContent = new JTextArea();
			jtfContent.setWrapStyleWord(true);
			jtfContent.setLineWrap(true);
			
			JScrollPane spContent = new JScrollPane(jtfContent);
			GridBagConstraints gbc_spContent = new GridBagConstraints();
			gbc_spContent.weightx = 0.9;
			gbc_spContent.weighty = 1.0;
			gbc_spContent.insets = new Insets(0, 0, 5, 10);
			gbc_spContent.fill = GridBagConstraints.BOTH;
			gbc_spContent.gridx = 1;
			gbc_spContent.gridy = 2;
			add(spContent, gbc_spContent);
			
			JPanel panel = new JPanel();
			panel.setOpaque(false);
			GridBagConstraints gbc_panel = new GridBagConstraints();
			gbc_panel.gridwidth = 2;
			gbc_panel.fill = GridBagConstraints.BOTH;
			gbc_panel.gridx = 0;
			gbc_panel.gridy = 3;
			add(panel, gbc_panel);
			GridBagLayout gbl_panel = new GridBagLayout();
			gbl_panel.columnWidths = new int[]{186, 75, 75, 0};
			gbl_panel.rowHeights = new int[]{20, 0};
			gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
			gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
			panel.setLayout(gbl_panel);
			
			JButton clearButton = new JButton("Clear");
			GridBagConstraints gbc_clearButton = new GridBagConstraints();
			gbc_clearButton.anchor = GridBagConstraints.NORTHWEST;
			gbc_clearButton.insets = new Insets(0, 0, 10, 20);
			gbc_clearButton.gridx = 1;
			gbc_clearButton.gridy = 0;
			panel.add(clearButton, gbc_clearButton);
			clearButton.setFont(clearButton.getFont().deriveFont(Font.BOLD).deriveFont(13.0f));
			clearButton.setPreferredSize(new Dimension(75,20));
			clearButton.setMinimumSize(new Dimension(75,20));
			clearButton.setBackground(new Color(170,170,170));
			clearButton.setForeground(new Color(30,30,30));
			clearButton.setOpaque(true);
			clearButton.setBorder(BorderFactory.createLineBorder(new Color(30,30,30)));
			
			JButton postButton = new JButton("Post");
			GridBagConstraints gbc_postButton = new GridBagConstraints();
			gbc_postButton.insets = new Insets(0, 20, 10, 0);
			gbc_postButton.anchor = GridBagConstraints.NORTHWEST;
			gbc_postButton.gridx = 2;
			gbc_postButton.gridy = 0;
			panel.add(postButton, gbc_postButton);
			postButton.setFont(postButton.getFont().deriveFont(Font.BOLD).deriveFont(13.0f));
			postButton.setPreferredSize(new Dimension(75,20));
			postButton.setMinimumSize(new Dimension(75,20));
			postButton.setBackground(new Color(170,170,170));
			postButton.setForeground(new Color(30,30,30));
			postButton.setOpaque(true);
			postButton.setBorder(BorderFactory.createLineBorder(new Color(30,30,30)));
			
			//ACTION LISTENER
			//clear action listner
			clearButton.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					clearAllInput();
				}
				
			});

			postButton.addActionListener(new ALAnnouncementPost());
			
			validate();
			repaint();
		}
		
		public void clearAllInput(){
			jtfTitle.setText("");
			jtfContent.setText("");
		}
		
		public String getTitle(){
			return jtfTitle.getText();
		}
		
		public String getContent(){
			if(jtfContent != null){
				return jtfContent.getText();
			} else{
				System.out.println("null jtfContent");
				return "null content";
			}
		}
		
		public CmsAnnouncement getComposedAnnouncement(){
			return new CmsAnnouncement(0, getTitle(), getContent(), new Date(),false); // ID and IsRead are ignored later on
		}
		
		public boolean validateAllInputs(){
			if(getTitle().equals("") && getContent().equals("")){
				JOptionPane.showMessageDialog(CmsPresenter.mainView,
					    "Announcement Title and Content are empty. Please enter a title and content for your new announcement",
					    "ERROR!",
					    JOptionPane.ERROR_MESSAGE);
				return false;
			}
			else return true;
		}
		
		//Paint rounded rectangle on the back ground
		@Override
		protected void paintComponent(Graphics g){
			super.paintComponent(g);
			g.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 7, 7);
		}
	}

	
	
}
